﻿// JScript 文件
var PubTree = null;
document.oncontextmenu=function(e){return false;} 
var major = null;
var minor = null;
var Shuxlist = null;    //记录要添加的结点的属性var tempid = null;



var typeArray = null;
var which = null;   
GetTypeArray();
function init()
{
    PubTree = new CtnTreeCtrl("PubTree", false);
    PubTree.create("PubTree");
    PubTree.show();
    
    PubTree.onExpand = onExpand;   //单击收缩图标
    PubTree.onRightMenu = onRightMenu;     //右键菜单
    
    PubTree.onSelectItem = onSelectItem;  //选择   
    
    major = document.getElementById("major").value;
    minor = document.getElementById("minor").value; 
    var url = "/main.ashx?";
    var method = "_method=InitTree&Major="+major+"&Minor="+minor+"&type=0";
    CtnTrans.Ajax(url,"post",method, ShowTree, PubTree, {loading:true});
}

function onSelectItem(item)
{
     if(document.getElementById("flag").value == "true")
     {
       //alert("123");
       var tool = new CtnTool();
      
       tool.id = m_vt.ECON_QRYCSTINFO;
       
       tool.name = "客户信息查询";
       tool.target = "main";
       tool.url = "EconAnalyse/QryEconAlyse.aspx?areaname="+ escape(item.text) + "&areaid=" +escape(item.data.id) +"&Check=4";
  /*
       url = "EconAnalyse/Query.aspx?areaname="+item.text + "&areaid=" +item.data.id;
       var params = {
                    title: "用户的消费信息",
                    bmax: false,
                    bmin:false,
                    bmodal: true,
                    width:700,
                    height:300,
                    bresize:true,
                    arrbtn: [{text:"关 闭", onClick:function(dlg){dlg.close();}}]
                    //data: para
                 }                    
                 
            var dlg = top.CtnDlg.open(url, params); 
          */ 
       top.m_doc.createWnd(tool);
     }
}

function onExpand(item)
{
    if(item.isdeal == false) 
    {
        item.loading();
        CtnTrans.Ajax("/main.ashx?", "post", "_method=InitTree&Major="+major+"&Minor="+minor+"&exp="+escape(item.data.exp)+"&gzid="+item.data.gzid, ShowTree, item);
    }    
}
function ShowTree(response, params)
{
    params.unloading();
    var xmlDoc  = response.responseText;
    if (!CtnError.validate(xmlDoc))
    {
        CtnError.show();
        return;
    }
    params.insertXml(xmlDoc);
    params.show();   
}
function onRightMenu(item,evt)
{
    vSelItem = item
    var chistrs = item.data.chistrs;
    mc = new CtnMenuCtrl("mc");
    mc.bOutHide  = true
    Shuxlist = Getchistrs(chistrs);
    
    if(item.data.id == "0")
    {
        with(mc)
        {
            for(var i = 0;i<Shuxlist.length;i++)    //动态添加右键菜单的添加结点项
            {
                addEvent(Shuxlist[i][4], "AddTool("+Shuxlist[i][0]+", "+Shuxlist[i][1]+", "+item.data.id+", "+Shuxlist[i][2]+", '"+Shuxlist[i][3]+"', "+i+")");
            }
        }
    }
    else if(chistrs == "")
    {
        with(mc)
        {
//            for(var i = 0;i<1;i++)
//            {
//              addEvent("手动录制覆盖范围", "AddTool("+item.data.mymajor+", "+item.data.myminor+", "+item.data.id+",4, '"+Shuxlist[i][3]+"', 0)");
//            }
            addEvent("浏览属性", "top.ExecuteATool(top.m_vt.FUN_ATT, ["+item.data.mymajor+", "+item.data.myminor+", "+item.data.id+", 'view', null])");
            addEvent("修改属性", "top.ExecuteATool(top.m_vt.FUN_ATT, ["+item.data.mymajor+", "+item.data.myminor+", "+item.data.id+", 'edit', EditTree])");
            addEvent("删除结点", "DeleteTree()");
            if(item.data.maptype != "0")
            {
                addEvent("定位图形", "top.ExecuteATool(top.m_vt.FUN_LOC,["+item.data.mymajor+","+item.data.myminor+","+item.data.id+"])");
            }
            SqJlRightMenu(item,mc,Shuxlist);
            //添加自动生成覆盖范围、手动录制覆盖范围、查看覆盖范围、修改覆盖范围        }
    }  
    else
    {
        with(mc)
        {            
            for(var i = 0;i<Shuxlist.length;i++)
            {
                addEvent(Shuxlist[i][4], "AddTool("+Shuxlist[i][0]+", "+Shuxlist[i][1]+", "+item.data.id+", "+Shuxlist[i][2]+", '"+Shuxlist[i][3]+"', "+i+")");
            }
            addEvent("浏览属性", "top.ExecuteATool(top.m_vt.FUN_ATT,["+item.data.mymajor+", "+item.data.myminor+", "+item.data.id+", 'view', null])");
            addEvent("修改属性", "top.ExecuteATool(top.m_vt.FUN_ATT,["+item.data.mymajor+", "+item.data.myminor+", "+item.data.id+", 'edit', EditTree])");
            addEvent("删除结点", "DeleteTree()");                                       
            if(item.data.maptype != "0")
            {
                addEvent("定位图形", "top.ExecuteATool(top.m_vt.FUN_LOC,["+item.data.mymajor+","+item.data.myminor+","+item.data.id+"])");
                addEvent("添加图形", "AddMapTool("+item.data.mymajor + "," + item.data.myminor + "," + item.data.id + "," + item.data.maptype + "," + item.data.mytype +")");
                addEvent("修改图形", "top.ExecuteATool(top.m_vt.KJZYGL_TXGL,[" + item.data.mymajor + ", " + item.data.myminor + ", " + item.data.id+"])");
                addEvent("删除图形", "top.ExecuteATool(top.m_vt.KJZYGL_SCTX,[" + item.data.mymajor + ", " + item.data.myminor + ", " + item.data.id+"])");
            }
            YwSqRightMenu(item, mc)    //营销区域新增右键菜单
        }
    }
    mc.show(evt);
}
function EditMap(mymajor,myminor,temid,maptype)  //修改图形
{
    top.ExecuteATool(top.m_vt.FUN_LOC,[mymajor,myminor,temid]);
    tempid = temid;
    var win = top.m_doc.getWndElem(top.m_vt.MAP_MAP);
    win.clearMap();
    EditMaptool(mymajor,myminor,temid,maptype);
}
function EditGraph(dot,temp)    
{
    vDot = dot;
    var vInfo = '<data major="'+temp[0]+'" minor="'+temp[1]+'"  temid="'+temp[2]+'">'
    vInfo += '<map>'
    for (var i = 0; i < vDot.length; ++i)
    {
        vInfo += '<item x="'+vDot[i].lx+'" y="'+vDot[i].ly+'">'
        vInfo += '</item>'
    }
    vInfo += '</map>'
    vInfo += "</data>"
    
    var xmlDoc = CtnTrans.getXml("/main.ashx?_method=EditMap", "post", escape(vInfo))
    top.CtnDlg.msg("修改成功!");
  
}
function EditMaptool(mymajor,myminor,temid,maptype)
{
     var win = top.m_doc.getWndElem(top.m_vt.MAP_MAP);
     switch (parseInt(maptype))
     {
         case 1://点         {
             top.CtnDlg.msg("请在地图上选择图形范围!")
             win.UserMapOper.setOperator(top.m_vt.MAP_DRAWLINE,{"Cb_DbClk" : EditGraph,"DbClk_Params":[mymajor,myminor,temid]});
         }
         break;
         case 2://线         {
             top.CtnDlg.msg("请在地图上选择图形范围!")
             win.UserMapOper.setOperator(top.m_vt.MAP_DRAWLINE,{"Cb_DbClk" : EditGraph,"DbClk_Params":[mymajor,myminor,temid]});
         }
         break;
         case 3://区         {
             top.CtnDlg.msg("请在地图上选择图形范围!")
             win.UserMapOper.setOperator(top.m_vt.MAP_DRAWPOLY,{"Cb_DbClk" : EditGraph,"DbClk_Params":[mymajor,myminor,temid]});
         }
         break;
         case 4://单独画区
         {
             top.CtnDlg.msg("请在地图上绘制图形范围!")
             win.UserMapOper.setOperator(top.m_vt.MAP_DRAWPOLY,{"Cb_DbClk" : EditGraph,"DbClk_Params":[mymajor,myminor,temid]});
         }
         break;
     }
}
function YwSqRightMenu(item, mc)   //营维社区部分右键菜单
{
    var major = item.data.mymajor
    var minor = item.data.myminor
    if(major == top.CtnMajorDef.TEL_MAJ_ATT && minor == top.CtnMinorDef.TEL_ATT_YXQY)
    { 
        with(mc)
        {
            addEvent("资源管理",  "top.ExecuteATool(top.m_vt.KJZYGL_YWBGL,[" + item.data.mymajor + ", " + item.data.myminor + ", " + item.data.id + ", " + item.data.glid + ", '" + escape(item.text) + "', 2])");
            addEvent("分配局站",  "top.ExecuteATool(top.m_vt.KJZYGL_YWBGL,[" + item.data.mymajor + ", " + item.data.myminor + ", " + item.data.id + ", " + item.data.glid + ", '" + escape(item.text) + "', 1])"); 
        }
    }
}

function DelYxSqNode(nodeType)
{
    top.CtnDlg.confirm("是否确认删除该节点？", function(flag){
        if(!flag)
        {
            return;
        }
        var vParam = "_method=delYwNode&id="+vSelItem.data.id + "&type=" + nodeType;
        var xmlDoc = CtnTrans.getText("/main.ashx", "get", vParam)
        if (!CtnError.validate(xmlDoc))
        {
            CtnError.show();
            return;
        }
        else
        {
            PubTree.deleteItem(vSelItem.idx)
        }
    })
}

function SqJlRightMenu(item, mc,Shuxlist)   //社区经理部分右键菜单
{
    var major = item.data.mymajor
    var minor = item.data.myminor
    if(major == top.CtnMajorDef.TEL_MAJ_ATT && minor == top.CtnMinorDef.TEL_ATT_YXQY)
    {
        with(mc)
        {
            addEvent("查看覆盖范围", "top.ExecuteATool(top.m_vt.FUN_LOC,["+item.data.mymajor+","+item.data.myminor+","+item.data.id+"])");
            addEvent("修改覆盖范围",  "top.ExecuteATool(top.m_vt.KJZYGL_YWBGL,[" + item.data.mymajor + ", " + item.data.myminor + ", " + item.data.id + ", " + item.data.glid + ", 'FGFW', 'EDIT'])"); 
            addEvent("自动生成覆盖范围",  "top.ExecuteATool(top.m_vt.KJZYGL_YWBGL,[" + item.data.mymajor + ", " + item.data.myminor + ", " + item.data.id + ", " + item.data.glid + ", 'FGFW', 'ZDBUILD'])"); 
            addEvent("手动录制覆盖范围", "AddTool("+item.data.mymajor+", "+item.data.myminor+", "+item.data.id+",4, '"+Shuxlist[0][3]+"', 0)");
        }
    }
}

function EditTree(dlg)
{
    var vGrid = dlg.wnd.getXml()
    var xmlDoc = CtnTrans.getText("/main.ashx?_method=EidtTree", "post", escape(vGrid))
    if (!CtnError.validate(xmlDoc))
    {
        CtnError.show();
    }
    else
    {
        var vXmlVal = CtnXML.getXmlDoc()
        vXmlVal.loadXML(xmlDoc)
        var vQyNodes = vXmlVal.selectNodes("item")
        vSelItem.setItemText($XML(vQyNodes[0]))   
    }
    dlg.close()
}
function AddTool(maj,min,id,map,dft,temp)
{
     which = temp;  
     vDft = dft;
     var win = top.m_doc.getWndElem(top.m_vt.MAP_MAP);
     switch (parseInt(map))
     {
         case 0:
         {
             top.ExecuteATool(top.m_vt.FUN_ATT, [maj, min, id, 'add', Add, dft])
         }
         break;
         case 1://点



         {
             top.CtnDlg.msg("请先在地图上选择图形范围!")
             win.UserMapOper.setOperator(top.m_vt.MAP_DRAWLINE,{"Cb_DbClk" : AddGraph});
         }
         break;
         case 2://线



         {
             top.CtnDlg.msg("请先在地图上选择图形范围!")
             win.UserMapOper.setOperator(top.m_vt.MAP_DRAWLINE,{"Cb_DbClk" : AddGraph});
         }
         break;
         case 3://区
         {
             top.CtnDlg.msg("请先在地图上选择图形范围!")
             win.UserMapOper.setOperator(top.m_vt.MAP_DRAWPOLY,{"Cb_DbClk" : AddGraph});
         }
         break;
         case 4://单独画区
         {
             top.CtnDlg.msg("请在地图上绘制图形范围!")
             win.UserMapOper.setOperator(top.m_vt.MAP_DRAWPOLY,{"Cb_DbClk" : AddGraphOnly});
         }
         break;
     }
}
function Add(dlg)
{
    var vGrid = dlg.wnd.getXml()
    var vInfo = '<data map="'+Shuxlist[which][2]+'" major="'+Shuxlist[which][0]+'" minor="'+Shuxlist[which][1]+'" glid="'+vSelItem.data.id+'" gzid="'+vSelItem.data.gzid+'" chitype="'+Shuxlist[which][5]+'">'
    if(Shuxlist[which][2] != "0")
    {   

        vInfo += '<map>'
        for (var i = 0; i < vDot.length; ++i)
        {
            vInfo += '<item x="'+vDot[i].lx+'" y="'+vDot[i].ly+'">'
            vInfo += '</item>'
        }
        vInfo += '</map>'
    }
    vInfo += vGrid
    vInfo += "</data>"
    var xmlDoc = CtnTrans.getXml("/main.ashx?_method=addTree", "post", escape(vInfo))
    if (!CtnError.validate(xmlDoc))
    {
        CtnError.show();
        return ;
    }
    else
    {
        if (vSelItem.isdeal == false)       //判断是否展开
        {
            PubTree.onExpand(vSelItem);
        }
        else
        {
            vSelItem.insertNode(CtnXML.getRootNode(xmlDoc));
            vSelItem.show();
        }
    }
    dlg.close()
}
function AddGraph(dot)
{
    vDot = dot;
    top.ExecuteATool(top.m_vt.FUN_ATT, [Shuxlist[which][0], Shuxlist[which][1], vSelItem.data.id, 'add', Add, Shuxlist[which][3]])
}
function AddGraphOnly(dot)//landeng
{
    vDot = dot;
    var vInfo = '<data map="'+vSelItem.data.mymajor+'" major="'+vSelItem.data.mymajor+'" minor="'+vSelItem.data.myminor+'" glid="'+vSelItem.data.id+'" gzid="'+vSelItem.data.gzid+'">'
    if(vSelItem.data.mymajor != "0")
    {
        vInfo += '<att><item>3</item></att>'
        vInfo += '<map>'
        for (var i = 0; i < vDot.length; ++i)
        {
            vInfo += '<item x="'+vDot[i].lx+'" y="'+vDot[i].ly+'">'
            vInfo += '</item>'
        }
        vInfo += '</map>'
    }
    vInfo += "</data>"
    // var vParam = "_method=AddQyGraph&maj="+vSelItem.data.mymajor+"&min="+vSelItem.data.myminor+"&glid="+vSelItem.data.id +"&gzid="+vSelItem.data.gzid;
    var xmlDoc = CtnTrans.getText("/main.ashx?_method=AddQyGraph", "post", escape(vInfo))
    if (!CtnError.validate(xmlDoc))
    {
        CtnError.show();
        return;
    }
    else
    {
        top.CtnDlg.msg("手动录制覆盖范围成功!");
    }
}
//获取即将添加的结点的属性

function Getchistrs(chistring)
{
    var chistrs = new Array();    
    var chistrlist = chistring.split('|');
    for(var i = 0;i<chistrlist.length;i++)
    {
        var everchi = chistrlist[i].split(',');
        var everchis = new Array();
        everchis[0] = everchi[0];
        everchis[1] = everchi[1];
        everchis[2] = everchi[2];
        everchis[3] = "<items><type>"+everchi[3]+"</type></items>";
        everchis[4] = "添加" + GetStrType(everchi[3]);
        everchis[5] = everchi[3];
        chistrs[i] = everchis;
    }     
    return chistrs;
}
function DeleteTree()
{
    if(vSelItem.data.mymajor == top.CtnMajorDef.TEL_MAJ_ATT && vSelItem.data.myminor == top.CtnMinorDef.TEL_ATT_YXQY)
    {
        var chistrs = vSelItem.data.chistrs;
        var nodeType = 1
        if(chistrs == "")
        {
            nodeType = 2
        }
        DelYxSqNode(nodeType)
        return;
    }
    if (confirm("请确认该区域下的实体已经清空，确认是否删除该区域？"))
    {
        var vParam = "_method=deleTree&maj="+vSelItem.data.mymajor+"&min="+vSelItem.data.myminor+"&id="+vSelItem.data.id;
        var xmlDoc = CtnTrans.getText("/main.ashx", "get", vParam)
        if (!CtnError.validate(xmlDoc))
        {
            CtnError.show();
            return;
        }
        else
        {
            PubTree.deleteItem(vSelItem.idx)
        }
    }
}
//获取所有级别的显示值

function GetTypeArray()
{
    var vParam = "_method=Gettypeval";
    var xmlDoc = CtnTrans.getText("/main.ashx", "get", vParam)
    var typeval = xmlDoc.split('|');
    typeArray = new Array();
    for(var i = 0;i<typeval.length;i++)
    {
        typeArray[i] = typeval[i].split(',');
    }
}
//获取type对应的显示值

function GetStrType(type)
{
    for(var i = 0;i<typeArray.length;i++)
    {
        if(type == typeArray[i][1])
        {
            return typeArray[i][0];
        }
    }
}

function AddMapTool(maj,min,id,map,mytype)
{
     var win = top.m_doc.getWndElem(top.m_vt.MAP_MAP);
     switch (parseInt(map))
     {
         case 1://点         {
             win.UserMapOper.setOperator(top.m_vt.MAP_DRAWLINE,{"Cb_DbClk" : AddMapGraph, "DbClk_Params":[maj,min,id,mytype]});
         }
         break;
         case 2://线         {
             win.UserMapOper.setOperator(top.m_vt.MAP_DRAWLINE,{"Cb_DbClk" : AddMapGraph, "DbClk_Params":[maj,min,id,mytype]});
         }
         break;
         case 3://区         {
             win.UserMapOper.setOperator(top.m_vt.MAP_DRAWPOLY,{"Cb_DbClk" : AddMapGraph, "DbClk_Params":[maj,min,id,mytype]});
         }
         break;
     }
}
function AddMapGraph(dot,temp)
{
    if (!temp) return;
    var win = top.m_doc.getWndElem(top.m_vt.MAP_MAP);
    
    var vInfo = "<data major='"+ temp[0] +"' minor='"+ temp[1] +"' id='"+temp[2]+"' mytype='"+temp[3]+"'>";
    vInfo += "<map>";
    for(var i = 0;i<dot.length;i++)
    {
        vInfo += "<item x='"+dot[i].lx+"' y='"+dot[i].ly+"'>";
        vInfo += "</item>";
    }
    vInfo += "</map>";
    vInfo += "</data>";
    var xmlDoc = CtnTrans.getXml("/main.ashx?_method=AddMap", "post", escape(vInfo));
    if(xmlDoc.text == "")
    {
//        var win = top.m_doc.getWndElem(m_vt.MAP_MAP);
//        var newmapgx = new Array();
//        for(var i = 0;i<dot.length;i++)
//        {
//            var vPnt1 = new top.D_Dot2D(dot[i].lx, dot[i].ly, false);
//            newmapgx.push(vPnt1)
//        }
//        win.MapGxOperObj.flashNodes.push(newmapgx);
//        win.MapGxOperObj.DrawFlashLin();
        top.CtnDlg.msg("添加成功!");
    }
    else if (!CtnError.validate(xmlDoc))
    {
        CtnError.show();
        return;
    }
}
////删除图形
//function DeleMap()
//{
//    //var gid = this.rectidList[window.rectIndex];        //图形ID
//    var win = m_doc.getWndElem(m_vt.MAP_MAP);
//    win.UserMapOper.ActiveTool = 20008;
//    win.UserMapOper.OperUp[win.UserMapOper.ActiveTool] = win.MapGxOperObj.DeleteDate.bind(win.MapGxOperObj);
//}
